<template>
<div class="add_modle_box">
    <div class="lin">
        <div class="lin_txt">
            模块名称：
        </div>
        <div class="lin_inp">
            <input v-model="name"/>
        </div>
    </div>
    <div class="lin">
        <div class="lin_txt">
            简介：
        </div>
        <div class="lin_inp">
            <textarea v-model="desc"></textarea>
        </div>
    </div>
    <div class="lin">
        <div class="lin_txt">
            System：
        </div>
        <div class="lin_inp">
            <input v-model="system"/>
        </div>
    </div>
    <div class="lin">
        <div class="lin_txt">
            prompt：
        </div>
        <div class="lin_inp">
            <textarea v-model="prompt" ></textarea>
        </div>
    </div>
    <div class="lin">
        <div class="lin_txt">
            金币花费：
        </div>
        <div class="lin_inp">
            <input v-model="score" type="number"/>
        </div>
    </div>
    <div class="lin">
        <div class="lin_txt">
            排序方案：
        </div>
        <div class="lin_inp">
            <input v-model="sort_index" type="number"/>
        </div>
    </div>
    <div class="lin flexBox">
        <div class="lin_txt">
            是否上线：
        </div>
        <div class="lin_inp">
            <select v-model="is_online" type="number">
                <option v-for="it in [1, 2, 3]" :value="it">
                    {{ it }}
                </option>
            </select>
        </div>
    </div>
    <div class="lin">
        <button @click="editModle">保存</button>
    </div>

</div>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import {editModleReq} from '../api/index.js'
import {useStore} from 'vuex'
const store = useStore()
const id = ref(0)
const name = ref('')
const desc = ref('')
const score = ref(0)
const system = ref('')
const prompt = ref('')
const sort_index = ref(1)
const is_online = ref(false)
onMounted(() => {
    let ms = store.state.modelList;
    let tobj = null;
    for (let i in ms) {
        let it = ms[i]
        if(it.id == store.state.curSelModel) tobj = it;
    }
    if (!tobj) return;
    id.value = tobj.id;
    name.value = tobj.name;
    desc.value = tobj.desc;
    score.value = tobj.score;
    system.value = tobj.system;
    prompt.value = tobj.prompt;
    sort_index.value = tobj.sort_index;
    is_online.value = tobj.is_online;
})
const editModle = () => {
    console.log('isonline', is_online.value);
    console.log('isonline', '4');
    let res = editModleReq(store.state.user.token, {
        id: id.value,name:name.value, desc:desc.value, score:score.value, 
        system:system.value, prompt:prompt.value, sort_index:sort_index.value, 
        is_online:is_online.value
    });
    res.then(resp=> {
        console.log('resp', resp);
        if (resp.data.code == 200) {
            alert('修改成功')
            router.push({
                path:'/main/userList'
            })
        }else {
            alert('修改失败')
        }
    }).catch(err=> {
        console.log(err);
    })
}

</script>

<style scoped>
.add_modle_box {
width: 400px;
}
.lin {
    line-height: 20px;
    margin-bottom: 20px;
    margin-left: 50%;
}
.flexBox{
    display: flex;
}
</style>